#!/usr/bin/env python3

import sys, json, os, operator
from common import fget, fset

files = [
#    target_file_name    extension    configurer
    ("Settings.java",    "oldfmt",    operator.mod),
]

def configure ():
    settings = json.loads(fget("settings.json"))
    for new_fname, ext, configurer in files:
        old_fname = new_fname + "." + ext
        old_content = fget(old_fname)
        new_content = configurer(old_content, settings)
        fset(new_fname, new_content)

def clean ():
    for new_fname, ext, configurer in files:
        clean_file(new_fname)

def clean_file (fname):
    try:
        os.remove(fname)
    except OSError as er:
        if er.errno == 2: # No such file or directory
            pass
        else:
            raise er



def main (argv0, argv):
    if argv and argv[0] == "clean":
        clean()
    else:
        configure()

if __name__ == "__main__":
    try:
        sys.exit(main(sys.argv[0], sys.argv[1:]))
    except KeyboardInterrupt:
        pass
